There are only a few HTML elements which apply to forms, but they allow you to get almost every kind of data from your visitors. Everything from buttons to checkboxes and input areas is available.
The data which the form generates when it is submitted or sent to the server is encoded in a special way. This is called form encoding, and it is discussed below.
Introduction to constructing forms |
Input fields: checkboxes, radio buttons... |
||
Selection lists |
Text input areas |
To illustrate how the various form elements work, you can look at the example form. The idea is to construct a form in which people can comment on this HTML Basics series. To make it easier to fill in and to process, most of the information will be made available with selection lists, although there will also be room for personalized comments.
METHOD
, which can be either GET or POST. The
second is ACTION
, which points to the URL for the processing
script on the server. It can be a relative URL, but most often it's
a full URL. This script is often called a "CGI script".
The method of POST allows unlimited amounts of data to be sent to the server. The data is sent in the body of the request for the form output and is presented on standard input to the processing script or program. The form input will not be shown when the form output is sent. Use POST if you have textareas on your form, or if you otherwise expect a large amount of data to be entered.
For all form elements, the input entered or selection made is
represented by a name and a value.
These two are combined into a pair. A pair is always of the form
name=value
. All pairs are sent to the server when the form is
submitted, in a compact, concatenated form:
name1=value&name2=value&name3=value
. It is up to
the server to split this concatenated list into something usable.
As the data entered by a user can contain reserved characters (such as the "=" or "&" character), the data can be encoded first. This encoding is simple: the character is replaced by its hexadecimal value in the ISO Latin 1 character set and prepended by the "%" character. The script can then turn it back into the actual character.
<INPUT TYPE=x NAME=y>
, where "x" denotes one of the
types discussed later, and NAME is a unique name for this input field.
You can also often use the VALUE attribute to provide an initial value,
and SIZE to suggest an appropriate size for the input field.
Of course, NAME, VALUE and SIZE are used for different purposes in every different type, and each type also has its own specific attributes.
Its main purpose is to allow the user to fill in one-line entries, such as name, e-mail address or phone number. Make sure the field is long enough for its purpose. If the user enters more data than your SIZE attribute indicates, the text will usually scroll insize the box, which makes it impossible to review the complete entered text at once.
"*"
characters is used for each
character entered. This provides some very basic method for a user
to enter his password safely. The password is still sent in the clear,
though.
In all other respects, it is identical to TYPE=text.
Checkboxes are most commonly used in yes/no queries. It is best to let the "on" state represent something positive, so you don't get into trouble with double negatives, although this depends on how you design your form. For example, "I want to receive e-mail when this page gets updated" accompanying a checkbox is more clear than "I do not want to receive e-mail when this page is updated" even though both serve the same purpose.
Note that if the checkbox is in the "off" state when the form is submitted, it is completely ignored in the form input. If it is in the "on" state, it is sent with a VALUE of "on" regardless of what the VALUE attribute says.
This allows for multiple-choice input, where all choices are mutually exclusive. Make sure that the choices are mutually exclusive, otherwise your reader might try to check more than one option at a time.
Make sure that you give exactly one of the radio buttons the attribute CHECKED, so it comes up as the default value. Without this, some browsers produce a list of radio buttons without any of them checked, which can produce unwanted side-effects when the form gets processed.
When the form gets submitted, the NAME of the series is sent together with the VALUE of the selected radio button.
The browser will simply send the NAME and VALUE of the hidden field to the server, with no modifications at all.
You can have more than one submit button on your form, although they all perform the same task. Make sure you give each submit button a different NAME, so that the browser can also tell you which submit button was pressed (with the NAME and VALUE of the button).
The VALUE of the submit button is often used as the text to display on the button. If it is not set, a value of "Submit" is used as a default.
In addition, the coordinates which the user clicked on are sent to the server. If, for example, the NAME is set to "myimage", then the browser would send "myimage.x=3" and "myimage.y=5" if the user clicked on the location (3,5) in the image.
The VALUE of the reset button is often used as the text to display on the button. If it is not set, a value of "Reset" or "Clear" is used as default.
The reset button is ignored completely when the form is submitted.
Typically, if the SIZE is set to one, the result is a "drop-down" box which presents a list of all the items. If it is set to more than one, the user can scroll through the items and pick the one(s) he wants.
Each element in the list is contained in an OPTION tag. An option may only contain plain text. Each option should have a unique NAME attribute. If you don't provide a NAME attribute for an option, the text which is displayed in the list is used as name. If you want the option to come up selected by default, use the SELECTED attribute.
When the form is submitted, the NAME of the select tag is combined with the name of the selected option and sent to the server.
<P> Please select the wine of your choice. <SELECT NAME=wine> <OPTION>Bordeaux <OPTION NAME=beau>Beaujolais <OPTION SELECTED>None </SELECT>In the case that the "Beaujolais" option is chosen, the value "beau" is sent to the server. In the other two cases, the actual text is used.
Unfortunately, many browsers have problems with line wrapping. This means that text entered by a user will not contain newlines unless the user explicitly entered them himself. This can result in very long lines, so you might want to rewrap the input in your processing script.
Default text for the text area can be specified by putting it inside the <TEXTAREA> and </TEXTAREA> tags. You can't use HTML tags here, but you should still encode characters such as &, < and > to prevent confusion. Line breaks you put in the default text should be retained when the text area is displayed.
Reference index ~
HTML Basics index ~
Feedback
Copyright © 1996 Arnoud "Galactus" Engelfriet.